home *** CD-ROM | disk | FTP | other *** search
- /* sendto.c
- #
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
- This software is copyright (C) by the Lawrence Berkeley Laboratory.
- Permission is granted to reproduce this software for non-commercial
- purposes provided that this notice is left intact.
-
- It is acknowledged that the U.S. Government has rights to this software
- under Contract DE-AC03-765F00098 between the U.S. Department of Energy
- and the University of California.
-
- This software is provided as a professional and academic contribution
- for joint exchange. Thus, it is experimental, and is provided ``as is'',
- with no warranties of any kind whatsoever, no support, no promise of
- updates, or printed documentation. By using this software, you
- acknowledge that the Lawrence Berkeley Laboratory and Regents of the
- University of California shall have no liability with respect to the
- infringement of other copyrights by any part of this software.
-
- For further information about this notice, contact William Johnston,
- Bld. 50B, Rm. 2239, Lawrence Berkeley Laboratory, Berkeley, CA, 94720.
- (wejohnston@lbl.gov)
-
- For further information about this software, contact:
- Jin Guojun
- Bld. 50B, Rm. 2275, Lawrence Berkeley Laboratory, Berkeley, CA, 94720.
- g_jin@lbl.gov
-
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Send images with options to a extended X server on a given host
- %
- % AUTHOR: Jin Guojun - LBL 1/1/93
- */
- char Usage[] =
- "\nusage: %s host [+udp] [+port #] [+rtp] [-d #] [-w #] [[| <] input] [-... options]",
- *Progname, *s_name="s_master";
-
- #include <sys/file.h>
- #include "net_need.h"
- #include "imagedef.h"
-
- #ifndef BufSize
- #define BufSize 32768
- #endif
-
- int window=Max_WINDOW_SIZE, total, udp, rtp, Delay; /* in micro sec. */
- static char buf[BufSize]=" connect";
- U_IMAGE uimg;
- #define isarg(s) !strncmp(av[n], s, 2)
- #define hdlen MAGIC_HEADER_LEN + sizeof(rtpmsg_hd)
-
- main(int ac, char* av[])
- {
- struct timeval ts;
- struct sockaddr_in server;
- char *hostp=buf+hdlen;
- int s=0, n=2, buf_size=BufSize;
- rtpmsg_hd* msg_hd;
-
- in_fp = stdin;
- Progname = *av;
-
- if (ac < n--)
- serr: prgmerr(n, Usage, *av);
-
- while (++n < ac)
- if (*av[n] == '+') {
- if (av[n][1]=='p' && n+1<ac)
- s_name = av[++n];
- else if (isarg("+u")) udp = True,
- window = UDP_BUF_LIMIT, buf_size = 28672;
- else if (isarg("+r")) rtp = True;
- else if (isarg("+d") && n+1<ac)
- Delay = atoi(av[++n]);
- else if (isarg("+w") && n+1<ac)
- window = atoi(av[++n]);
- else goto serr;
- } else if (*av[n] == '-') {
- strcat(hostp, av[n]);
- strcat(hostp, " ");
- if (n+1<ac && *av[n+1] != '-' && *av[n+1] != '+')
- strcat(hostp, av[++n]),
- strcat(hostp, " ");
- strcpy(buf, "#"); /* arg is set */
- } else s = n; /* file name position */
-
- if (s && !(in_fp=fopen(av[s], "rb")))
- goto serr;
- io_test(fileno(in_fp), goto serr);
-
- n = strlen(hostp); hostp = av[1];
- s = build_socket(hostp, s_name, udp<IP_SOCK ? udp+1 : IP_SOCK,
- True, &window, &server, 0);
- if (!udp)
- if (connect(s, &server, sizeof(server)) /* for write, but sendto */
- < 0) prgmerr(1, buf);
- else message("%s: connected to %s, port=%d <window %d>\n",
- getenv("HOSTNAME"), hostp, server.sin_port, window);
- else message("send UDP to %s\n", hostp);
-
- msg_hd = (rtpmsg_hd*) &buf[MAGIC_HEADER_LEN];
- ((short*)buf)[1] = htons(n); /* params len */
- n += hdlen + 1; /* for a NULL ending */
- if (buf_size > window)
- buf_size = window;
- window = buf_size;
-
- gettimeofday(&ts, 0);
- msg_hd->tss_s = htonl(ts.tv_sec);
- msg_hd->tss_us = htonl(ts.tv_usec);
-
- if (!strcmp(buf, "#"))
- if ((total=!udp ? write(s, buf, n) :
- sendto(s, buf, n, 0, &server, sizeof(server))) != n)
- syserr("sendto header to");
- else message("send params {%s} to %s\n", buf+hdlen, hostp);
- do {
- if (rtp) buf_size = read(fileno(in_fp), buf, window);
- else buf_size = fread(buf, 1, window, in_fp);
- if ((n = !udp ? write(s, buf, buf_size) :
- sendto(s, buf, buf_size, 0, &server, sizeof(server))) < 1)
- syserr("sento");
- total += n;
- if (Delay) {
- ts.tv_sec = 0; ts.tv_usec = Delay;
- select(1, 0, 0, 0, &ts);
- }
- #ifdef _DEBUG_
- message("\r%d Bytes send to %s", total, hostp);
- #endif
- } while (n == window || (rtp && n > 0));
- message("\rTotal %d Bytes send to %s\n", total, hostp);
- }
-